﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using SoSlam.Common.ClientLogic.ScreenManager;

namespace SoSlam.Common.ClientLogic.Screens
{
    class SplashScreen : GameScreen
    {
        #region Fields

        bool loadingIsSlow;
        bool otherScreensAreGone;
        Texture2D _SoNutsSplash;
        TimeSpan splashScreenLife;
        TimeSpan currentScreenTime;
        GameScreen[] screensToLoad;

        #endregion

        #region Initialization

        /// <summary>
        /// 
        /// </summary>
        public SplashScreen()
        {
            GameScreen[] gs = new GameScreen[2];

            //I would like to load main menu screen after this one
            gs.SetValue(new BackgroundScreen(), 0);
            gs.SetValue(new MainMenuScreen(), 1);
            this.loadingIsSlow = true;
            screensToLoad = gs;

            //how long does screen state active
            splashScreenLife = TimeSpan.FromSeconds(3.0);
            TransitionOnTime = TimeSpan.FromSeconds(1);
            TransitionOffTime = TimeSpan.FromSeconds(1.5);
        }


        /// <summary>
        /// Activates the loading screen.
        /// </summary>
        public static void Load(SoSlam.Common.ClientLogic.ScreenManager.ScreenManager screenManager, bool loadingIsSlow,
                                PlayerIndex? controllingPlayer,
                                params GameScreen[] screensToLoad)
        {
            // Tell all the current screens to transition off.
            foreach (GameScreen screen in screenManager.GetScreens())
                screen.ExitScreen();

            // Create and activate the loading screen.
            SplashScreen loadingScreen = new SplashScreen();

            screenManager.AddScreen(loadingScreen, controllingPlayer);
        }


        #endregion

        #region Update and Draw


        /// <summary>
        /// Updates the loading screen.
        /// </summary>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            // If all the previous screens have finished transitioning
            // off, it is time to actually perform the load.
            if (otherScreensAreGone)
            {
                ScreenManager.RemoveScreen(this);

                foreach (GameScreen screen in screensToLoad)
                {
                    if (screen != null)
                    {
                        ScreenManager.AddScreen(screen, ControllingPlayer);
                    }
                }

                // Once the load has finished, we use ResetElapsedTime to tell
                // the  game timing mechanism that we have just finished a very
                // long frame, and that it should not try to catch up.
                ScreenManager.Game.ResetElapsedTime();
            }
        }

        public override void HandleInput(InputState input)
        {
            PlayerIndex pi = new PlayerIndex();
            if (input.IsMenuSelect(PlayerIndex.One, out pi))
                CloseSplashScreen();

            base.HandleInput(input);
        }

        public void CloseSplashScreen()
        {
            this.ExitScreen();
            ScreenManager.AddScreen(new BackgroundScreen(), PlayerIndex.One);
            ScreenManager.AddScreen(new MainMenuScreen(), PlayerIndex.One);
        }

        /// <summary>
        /// Draws the loading screen.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            currentScreenTime += gameTime.ElapsedGameTime;
            // If we are the only active screen, that means all the previous screens
            // must have finished transitioning off. We check for this in the Draw
            // method, rather than in Update, because it isn't enough just for the
            // screens to be gone: in order for the transition to look good we must
            // have actually drawn a frame without them before we perform the load.
            if ((ScreenState == ScreenState.Active) && (ScreenManager.GetScreens().Length == 1))
            {
                //only transition if splash screen life has been reached
                if (currentScreenTime > splashScreenLife)
                    otherScreensAreGone = true;
            }

            // The gameplay screen takes a while to load, so we display a loading
            // message while that is going on, but the menus load very quickly, and
            // it would look silly if we flashed this up for just a fraction of a
            // second while returning from the game to the menus. This parameter
            // tells us how long the loading is going to take, so we know whether
            // to bother drawing the message.
            if (loadingIsSlow)
            {
                SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
                Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
                Rectangle fullscreen = new Rectangle(0, 0, viewport.Width, viewport.Height);

                _SoNutsSplash = ScreenManager.Game.Content.Load<Texture2D>("SplashScreen");

                spriteBatch.Begin();
                spriteBatch.Draw(_SoNutsSplash, fullscreen, new Color(TransitionAlpha, TransitionAlpha, TransitionAlpha));
                spriteBatch.End();
            }
        }
        #endregion
    }
}
